using SG3L_RAG.Application.Interfaces;
using SG3L_RAG.Domain.Entities;
using SG3L_RAG.Domain.Repositories;

namespace SG3L_RAG.Application.Services
{
    public class StatisticsService : IStatisticsService
    {
        private readonly IRepository<Statistics> _statisticsRepository;

        public StatisticsService(IRepository<Statistics> statisticsRepository)
        {
            _statisticsRepository = statisticsRepository;
        }

        public async Task<Statistics?> GetByIdAsync(Guid id)
        {
            return await _statisticsRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<Statistics>> GetAllAsync()
        {
            return await _statisticsRepository.GetAllAsync();
        }

        public async Task<Statistics> CreateAsync(string statType, int statValue)
        {
            var statistics = Statistics.Create(statType, statValue);
            await _statisticsRepository.AddAsync(statistics);
            return statistics;
        }

        public async Task UpdateAsync(Statistics statistics)
        {
            await _statisticsRepository.UpdateAsync(statistics);
        }

        public async Task DeleteAsync(Guid id)
        {
            await _statisticsRepository.DeleteAsync(id);
        }

        public async Task<IEnumerable<Statistics>> GetByTypeAsync(string statType)
        {
            var statistics = await _statisticsRepository.GetAllAsync();
            return statistics.Where(s => s.StatType == statType);
        }

        public async Task<Statistics?> GetLatestByTypeAsync(string statType)
        {
            var statistics = await GetByTypeAsync(statType);
            return statistics.OrderByDescending(s => s.CreatedAt).FirstOrDefault();
        }

        public async Task<Statistics> CreateUserCountAsync(int count)
        {
            return await CreateAsync("user_count", count);
        }

        public async Task<Statistics> CreateDocumentCountAsync(int count)
        {
            return await CreateAsync("document_count", count);
        }

        public async Task<Statistics> CreateConversationCountAsync(int count)
        {
            return await CreateAsync("conversation_count", count);
        }

        public async Task<Statistics> CreateMessageCountAsync(int count)
        {
            return await CreateAsync("message_count", count);
        }

        public async Task IncrementStatAsync(string statType)
        {
            var latest = await GetLatestByTypeAsync(statType);
            var newValue = (latest?.StatValue ?? 0) + 1;
            await CreateAsync(statType, newValue);
        }

        public async Task DecrementStatAsync(string statType)
        {
            var latest = await GetLatestByTypeAsync(statType);
            var newValue = Math.Max(0, (latest?.StatValue ?? 0) - 1);
            await CreateAsync(statType, newValue);
        }

        public async Task UpdateStatValueAsync(string statType, int newValue)
        {
            await CreateAsync(statType, newValue);
        }
    }
}
